package com.njmaomao.gj.pyot.domain.wechat.wechatapi;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * 退款结果通知
 * @author hh 2018-5-10
 */
@XStreamAlias("xml")
public class RefundNotification {

    // 返回状态码 必填[是 ] SUCCESS/FAIL 此字段是通信标识，非结果标识，退款是否成功需要解密后查看refund_status 来判断
    @XStreamAlias("return_code")
    private String returnCode;

    // 返回信息 必填[否 ] 返回信息，如非空，为错误原因  参数格式校验错误
    @XStreamAlias("return_msg")
    private String returnMsg;

    // 公众账号ID 必填[是 ] 微信分配的公众账号ID（企业号corpid即为此appId）
    private String appid;

    // 退款的商户号 必填[是 ] 微信支付分配的商户号
    @XStreamAlias("mch_id")
    private String mchId;

    // 随机字符串 必填[是 ] 随机字符串，不长于32位。推荐随机数生成算法
    @XStreamAlias("nonce_str")
    private String nonceStr;

    // 加密信息 必填[是 ] 加密信息请用商户秘钥进行解密，详见解密方式
    @XStreamAlias("req_info")
    private String reqInfo ;

    //TODO 以下为返回的加密字段：
    // 微信订单号必填[是 ] 微信订单号
    @XStreamAlias("transaction_id")
    private String transactionId;

    // 商户订单号 必填[是 ] 商户系统内部的订单号
    @XStreamAlias("out_trade_no")
    private String outTradeNo;

    // 微信退款单号 必填[是 ] 微信退款单号
    @XStreamAlias("refund_id")
    private String refundId;

    // 商户退款单号 必填[是 ] 商户退款单号
    @XStreamAlias("out_refund_no")
    private String outRefundNo;

    // 订单金额 必填[是 ] 订单总金额，单位为分，只能为整数，详见支付金额
    @XStreamAlias("total_fee")
    private int totalFee;

    // 应结订单金额 必填[否 ] 当该订单有使用非充值券时，返回此字段。应结订单金额=订单金额-非充值代金券金额，应结订单金额<=订单金额。
    @XStreamAlias("settlement_total_fee")
    private int settlementTotalFee;

    // 申请退款金额 必填[是 ] 退款总金额,单位为分
    @XStreamAlias("refund_fee")
    private int refundFee;

    // 退款金额 必填[是 ] 退款金额=申请退款金额-非充值代金券退款金额，退款金额<=申请退款金额
    @XStreamAlias("settlement_refund_fee")
    private int settlementRefundFee;

    // 退款状态 必填[是 ] SUCCESS-退款成功 CHANGE-退款异常 REFUNDCLOSE—退款关闭
    @XStreamAlias("refund_status")
    private String refundStatus;

    // 退款成功时间 必填[否 ] 资金退款至用户帐号的时间，格式2017-12-15 09:46:01
    @XStreamAlias("success_time")
    private String successTime;

    /**
     * 退款入账账户 必填[是 ] 取当前退款单的退款入账方
     * 取当前退款单的退款入账方
     *
     * 1）退回银行卡：
     *
     * {银行名称}{卡类型}{卡尾号}
     *
     * 2）退回支付用户零钱:
     *
     * 支付用户零钱
     *
     * 3）退还商户:
     *
     * 商户基本账户
     *
     * 商户结算银行账户
     *
     * 4）退回支付用户零钱通:
     *
     * 支付用户零钱通
     */
    @XStreamAlias("refund_recv_accout")
    private String refundRecvAccout;

    // 退款资金来源 必填[是 ] REFUND_SOURCE_RECHARGE_FUNDS 可用余额退款/基本账户 REFUND_SOURCE_UNSETTLED_FUNDS 未结算资金退款
    @XStreamAlias("refund_account")
    private String refundAccount;

    // 退款发起来源 必填[是 ] API接口 VENDOR_PLATFORM商户平台
    @XStreamAlias("refund_request_source")
    private String refundRequestSource;

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getReqInfo() {
        return reqInfo;
    }

    public void setReqInfo(String reqInfo) {
        this.reqInfo = reqInfo;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public String getRefundId() {
        return refundId;
    }

    public void setRefundId(String refundId) {
        this.refundId = refundId;
    }

    public String getOutRefundNo() {
        return outRefundNo;
    }

    public void setOutRefundNo(String outRefundNo) {
        this.outRefundNo = outRefundNo;
    }

    public int getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(int totalFee) {
        this.totalFee = totalFee;
    }

    public int getSettlementTotalFee() {
        return settlementTotalFee;
    }

    public void setSettlementTotalFee(int settlementTotalFee) {
        this.settlementTotalFee = settlementTotalFee;
    }

    public int getRefundFee() {
        return refundFee;
    }

    public void setRefundFee(int refundFee) {
        this.refundFee = refundFee;
    }

    public int getSettlementRefundFee() {
        return settlementRefundFee;
    }

    public void setSettlementRefundFee(int settlementRefundFee) {
        this.settlementRefundFee = settlementRefundFee;
    }

    public String getRefundStatus() {
        return refundStatus;
    }

    public void setRefundStatus(String refundStatus) {
        this.refundStatus = refundStatus;
    }

    public String getSuccessTime() {
        return successTime;
    }

    public void setSuccessTime(String successTime) {
        this.successTime = successTime;
    }

    public String getRefundRecvAccout() {
        return refundRecvAccout;
    }

    public void setRefundRecvAccout(String refundRecvAccout) {
        this.refundRecvAccout = refundRecvAccout;
    }

    public String getRefundAccount() {
        return refundAccount;
    }

    public void setRefundAccount(String refundAccount) {
        this.refundAccount = refundAccount;
    }

    public String getRefundRequestSource() {
        return refundRequestSource;
    }

    public void setRefundRequestSource(String refundRequestSource) {
        this.refundRequestSource = refundRequestSource;
    }

    /**
     * 获取Date格式的退款成功时间。
     * 如果获取错误，将返回当前时间
     * @return
     */
    public Date getSuccessTimeForDate(){
        try {
            return DateUtils.parseDate(getSuccessTime(),"yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            return new Date();
        }
    }
    public boolean success(){
        return StringUtils.equalsIgnoreCase(getReturnCode(),"SUCCESS")
                && StringUtils.equalsIgnoreCase(getRefundStatus(),"SUCCESS");
    }
}
